package com.itheima.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.Service.CheckGroupService;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.CheckGroupAndItemDao;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.SetmealAndCheckGroupDao;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckGroupAndItemDao checkGroupAndItemDao;
    @Autowired
    private SetmealAndCheckGroupDao setmealAndCheckGroupDao;
    /**
     * 新增检查组
     * @return
     */
    @Override
    @Transactional  //加上事务
    public void add(CheckGroup checkGroup, String checkitemIds) {
        //数据库添加检查组,并返回id
        checkGroupDao.insert(checkGroup);

        if (checkitemIds ==null || checkitemIds.equals("")){
            return;
        }else {
            //向关联表添加数据
            String[] splits = checkitemIds.split(",");
            for (String split : splits) {
                Integer checkitemId=Integer.parseInt(split);
                checkGroupAndItemDao.insert(checkGroup.getId(),checkitemId);
            }
        }

    }
    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(String id) throws Exception {
        Integer checkGroupId=Integer.parseInt(id);
        //先查t_setmeal_checkgroup表是否关联了checkgroupID，关联了就不能删，或者需要先删setmeal表；
        List<Integer> setmealIdsList= setmealAndCheckGroupDao.findByCheckGroupId(checkGroupId);

        if (setmealIdsList==null || setmealIdsList.size()==0){
            //删除关联表
            checkGroupAndItemDao.deleteBycheckGroupId(checkGroupId);
            setmealAndCheckGroupDao.deleteByCheckGroupId(checkGroupId);
            //删除检查组表
            checkGroupDao.deleteById(checkGroupId);

        }else {
            throw new Exception("与setmeal套餐表有关联关系，不能删除");
        }

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(String id) {
        Integer checkGroupId=Integer.parseInt(id);
        CheckGroup checkGroup= checkGroupDao.findById(checkGroupId);
        return checkGroup;
    }
    /**
     * 修改检查组
     * @param
     * @return
     */
    @Override
    @Transactional
    public void update(CheckGroup checkGroup,String checkitemIds) throws Exception {
        //修改检查组
        checkGroupDao.update(checkGroup);

        if (checkitemIds ==null){
            throw new Exception("checkitemIds为null");
        }else {
            //先删除关联的检查项所有，再加回来
            checkGroupAndItemDao.deleteBycheckGroupId(checkGroup.getId());
            String[] splits = checkitemIds.split(",");
            for (String split : splits) {
                Integer checkitemId=Integer.parseInt(split);
                checkGroupAndItemDao.insert(checkGroup.getId(),checkitemId);
            }
        }

    }

    /**
     * 分页查询,根据项目编码/项目名称分页查询检查组
     * @return
     */
    @Override
    public PageResult PageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<CheckGroup> checkGroupList= checkGroupDao.getList(queryPageBean.getQueryString()); //模糊查询
        Page<CheckGroup> page= (Page<CheckGroup>) checkGroupList;

        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 查询所有检查组
     * @return
     */
    @Override
    public List<CheckGroup> getAll() {
        List<CheckGroup>  checkGroupList= checkGroupDao.getList(null);
        return checkGroupList;
    }

    /**
     * 根据套餐id查询关联的检查组id
     * @param setmealId
     * @return
     */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(String setmealId) {
        Integer setmealId0=Integer.parseInt(setmealId);
        List<Integer> CheckGroupIds= checkGroupDao.findCheckGroupIdsBySetmealId(setmealId0);
        return CheckGroupIds;
    }


}
